package com.itboye.ihomebank.home;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.custom.FlowLayout;
import com.itboye.ihomebank.util.ByAlert;

/**
 * 合同3
 * 
 */
public class ActivityTianXieHeTongThree extends BaseOtherActivity {
	ImageView close_icon;
	TextView add_lift_tv;

	Button yulan, next;
	FlowLayout flowLayout;
	TextView save;
	/** 点击标签的次数 */
	private int dian = 1;

	String[] biao = { "电视机", "冰箱", "煤气炉", "空调", "电脑", "写字桌", "双人床" };
	ArrayList<TextView> arrayListTitle = new ArrayList<>();
	ArrayList<TextView> arrayListNum = new ArrayList<>();
	StringBuffer selectedDevice=new StringBuffer();
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.save:
			HashMap<String, String> hashMap = new HashMap<>();
			for (int i = 0; i < arrayListTitle.size(); i++) {
				if (arrayListNum.get(i).getVisibility() == View.VISIBLE) {
					selectedDevice.append(arrayListTitle.get(i).getText().toString()+"x"+arrayListNum.get(i).getText().toString()+"、");
//					hashMap.put(arrayListTitle.get(i).getText().toString(),
//							arrayListNum.get(i).getText().toString());
				}
			}
			intent=new Intent();
			intent.putExtra("selectDevice", selectedDevice.length()>0?selectedDevice.toString().substring(0, selectedDevice.length()-1):"");
			setResult(102, intent);
			System.out.println("select"+selectedDevice);
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		return R.layout.activity_tianxiehetong_three;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		add_lift_tv.setText("选择资产清单");
		save.setVisibility(View.VISIBLE);
		// TODO Auto-generated method stub
		for (int i = 0; i < biao.length; i++) {
			View view = LayoutInflater.from(this).inflate(R.layout.item_jiaju,
					null);
			final TextView num = (TextView) view.findViewById(R.id.num);
			arrayListNum.add(num);
			final TextView btn_name = (TextView) view
					.findViewById(R.id.btn_name);
			arrayListTitle.add(btn_name);

			btn_name.setText(biao[i]);

			btn_name.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					ByAlert.alert("点击的按钮");

					dian++;
					if (dian % 2 == 0) {
						btn_name.setBackgroundResource(R.drawable.shape_blue02);
						num.setVisibility(View.VISIBLE);
					} else {
						btn_name.setBackgroundResource(R.drawable.shape_gray);
						num.setVisibility(View.GONE);
						num.setText(1 + "");
					}
				}
			});
			num.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ByAlert.alert("点击的数字");
					num.setText((Integer.parseInt(num.getText().toString()) + 1)
							+ "");
				}
			});
			MarginLayoutParams layoutParams = new MarginLayoutParams(
					MarginLayoutParams.WRAP_CONTENT,
					MarginLayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(10, 10, 10, 10);
			flowLayout.addView(view, layoutParams);
		}
	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

}
